// user.ts
import { Commit } from "vuex";
import { UserState } from "../interface";
import { useStorage } from "@/library/storage";

const { getStorage } = useStorage("localStorage");

const cache = getStorage("user");

const state: UserState = {
  // 初始状态
  user: cache ? { ...cache } : { UserId: undefined, Username: "" },
  config: {},
};

const mutations = {
  // mutations
  SET_USER(state: UserState, value: UserState['user']) {
    state.user = { ...value };
  },
};

const actions = {
  // actions
  setUser({ commit }: { commit: Commit }, value: UserState['user']) {
    commit("SET_USER", value);
  },
};

const getters = {
  // getters
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
